//
//  HXLDomesticView.m
//  On The Load
//
//  Created by 晓琳 on 15/11/8.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLDomesticView.h"
#import "HXLDomTableViewCell.h"
#import "BaseLabel.h"
#import "DataHandle.h"
#import "HXLCityModel.h"
#import "HXLLeavesModel.h"

@interface HXLDomesticView ()<UITableViewDataSource, UITableViewDelegate,DomesticViewControllerPushPlaceDetailViewControllerDelegate>
@property (nonatomic ,strong) BaseTableView *domesticTV;
@property (nonatomic, strong) NSMutableArray *placeArray;
@property (nonatomic, strong) NSMutableDictionary *cityDic;


@end

@implementation HXLDomesticView

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void) createSubviews {
    
    self.domesticTV = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.frame.size.height-49) style:UITableViewStyleGrouped];
    
    self.domesticTV.delegate = self;
    self.domesticTV.dataSource = self;
    [self addSubview:self.domesticTV];
    [self.domesticTV reloadData];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    if (_domesticTV == tableView) {
        return 2;
    } else {
    return 1;
    }
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_domesticTV == tableView) {
        return 1;
    } else {
        return self.cityModel.leavesArray.count;
    }}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (_domesticTV == tableView) {
        
    
    BaseView *headView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40*SCALEH)];
//    headView.backgroundColor = [UIColor lightGrayColor];
    BaseLabel *headLable = [[BaseLabel alloc] initWithFrame:CGRectMake(20*SCALEW, 10*SCALEH , 100*SCALEW, 20*SCALEH)];
    [headView addSubview:headLable];
    if (0 == section) {
        headLable.text = @"热门城市";
    } else {
        headLable.text = @"快速检索";
    }
    
    return headView;
    }else {
        return nil;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _domesticTV) {
        
    
    NSString *cellIdentifer = @"domCell";
    HXLDomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];

    
    if (!cell) {
        cell = [[HXLDomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    }
    self.placeArray = [[DataHandle shareDataHandle].finDic objectForKey:@"areas"];
    HXLCityModel *model = [self.placeArray objectAtIndex:0];

    if (indexPath.section == 0) {
        //热门城市
        cell.cityModel = model;
        cell.height = 160*SCALEH;
        cell.myDelegate = self;
    }else{
        [self.placeArray removeObject:model];

        cell.myDelegate = self;
        cell.cityArray = self.placeArray;
        cell.height = 280*SCALEH;
        cell.cityModel = nil;
    }
    

    
    return cell;
    }else {
        NSString *cellIdentifer = @"detailCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
            
        }
        HXLLeavesModel *smallModel = [self.cityModel.leavesArray objectAtIndex:indexPath.row];
        cell.textLabel.text = smallModel.cityName;
        return cell;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (_domesticTV == tableView) {
       return 40*SCALEH;
    }else {
        return 0;
    }
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_domesticTV == tableView) {
        
    
    if (0 == indexPath.section) {
        return 160*SCALEH;
    }else {
        return 280*SCALEH;
    }
    }else {
        return 50*SCALEH;
    }
}

- (void) getModelValueForPlanViewController:(HXLLeavesModel *)model {
    
    self.smallModel = model;
    [self.domViewDelegate domViewPopPlaceViewController:model];
}




//实现协议方法,创建tableView
- (void) getTableViewForModel:(HXLCityModel *)model {
    
    self.detailTV = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.detailTV.delegate = self;
    self.detailTV.dataSource = self;
    [self addSubview:self.detailTV];
    self.cityModel = model;
    


}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.detailTV == tableView) {
        HXLLeavesModel *model = [self.cityModel.leavesArray objectAtIndex:indexPath.row];
        [self.domViewDelegate domViewPopPlaceViewController:model];
    }
}

@end
